package com.qfedu.controller;

import com.qfedu.common.JsonResult;
import com.qfedu.entity.User;
import com.qfedu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * projectName: backStage
 * author: CGY
 * time: 2020/9/25 21:18
 * description:
 */
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;
    @RequestMapping("/login.do")
    @ResponseBody
    public JsonResult login(String no, String pass, HttpSession session){
        User user = loginService.login (no, pass);
        session.setAttribute ("loginUser",user);
        return new JsonResult (1,user.getIdentity ());
    }

    /*
     * 用来验证主页是否登录
     * */
    @RequestMapping("LoginToJudge.do")
    @ResponseBody
    public JsonResult LoginToJudge(HttpSession session){
        User user = (User) session.getAttribute ("loginUser");
        if(user==null){
            return new JsonResult (0,"未登录");
        }else{
            return new JsonResult (1,user.getIdentity ());
        }
    }
    /*
     * 注销用户功能
     * */
    @RequestMapping("loginOut.do")
    @ResponseBody
    public JsonResult loginOut(HttpSession session){
        session.invalidate ();
        return new JsonResult (1,"注销成功");
    }

}
